package com.zxy.ider.ehome.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.ider.deskcust.TileEntry;
import com.zxy.ider.ehome.Appcontext;
import com.zxy.ider.ehome.R;
import com.zxy.ider.ehome.View.TitleTextView;

/**
 * Created by ider-eric on 2017/5/27.
 */

public class TvhGridAdapter extends BaseAdapter {
    private TileEntry[] tileEntrys;

    public TvhGridAdapter(TileEntry[] tileEntrys) {
        this.tileEntrys = tileEntrys;
    }

    @Override
    public int getCount() {
        return tileEntrys.length;
    }

    @Override
    public Object getItem(int i) {
        return tileEntrys[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GridViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new GridViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.homegrid_item, parent, false);
            viewHolder.iconview = (ImageView) convertView.findViewById(R.id.tool_icon);
            viewHolder.title = (TextView) convertView.findViewById(R.id.tool_title);
            viewHolder.iconbg = (RelativeLayout) convertView.findViewById(R.id.icon_bg);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GridViewHolder) convertView.getTag();
        }
        Glide.with(Appcontext.getAppContext())
                .load(tileEntrys[position].iconbg)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>(1920,1080) {//设置宽高
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Drawable drawable = new BitmapDrawable(resource);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            viewHolder.iconbg.setBackground(drawable);//设置背景
                        }
                    }
                });
        Glide.with(Appcontext.getAppContext()).load(tileEntrys[position].strupic).error(R.mipmap.defaulterr).crossFade().into(viewHolder.iconview);//.transform(new GlideCircleTransform(Appcontext.getAppContext()))
        viewHolder.title.setText(tileEntrys[position].strutext);
//        viewHolder.title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
       // Glide.with(Appcontext.getAppContext()).load(tileEntrys[position].strutitle).crossFade().into(viewHolder.titleview);
        return convertView;
    }

    class GridViewHolder {
        public ImageView iconview;
        public TextView title;
        public RelativeLayout iconbg;
    }

}
